﻿  using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Text;

using System.Xml;
using System.Xml.Serialization;
using cz.uhul.DSMLv2;
using cz.uhul.DSMLv2.Server.Sql;

namespace testDSML
{
  public class testRequest1
  {

    public static void go()
    {
      LdRequestDocument rq = new LdRequestDocument()
      {
        AuthRequest = new LdAuthRequest() { Principal = "tst" }
        ,AddRequests = new LdRequestsCollection()
        {
          new LdAddRequest()
          {
            DistinguishedName = "cn=eagriSync,cn=schema,cn=config"
            ,
            Attributes = new LdAttrCollection()
            {
              new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "top" //inherits from top
                }
              }
              ,new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "group" //auxiliarity from group (member)
                }
              }
              ,new LdAttr()
              {
                Name = "mod"
                ,Values = new ObservableCollection<string>()
                {
                  "STRUCTURAL" //it is instantiable
                }
              }
            }
          }
          ,new LdAddRequest()
          {
            DistinguishedName="cn=aktualizace eagri uživatelů,cn=exe"
            ,Attributes = new LdAttrCollection()
            {
              new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "eagriSync"
                }
              }
              ,new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "group"
                }
              }
              ,new LdAttr()
              {
                Name = "member"
                ,Values = new ObservableCollection<string>()
                {
                  "Alice"
                }
              }
            }
          }
          ,new LdAddRequest()
          {
            DistinguishedName="CN=Bob,OU=HR1,DC=Example,DC=COM"
            ,Attributes = new LdAttrCollection()
            {
              new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "eagriUzivatel"
                }
              }
              ,new LdAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "bob"
                }
              }
            }
          }
          ,new LdAddRequest()
          {
            DistinguishedName="CN=Alice,OU=HR1,DC=Example,DC=COM"
            ,Attributes = new LdAttrCollection()
            {
              new LdAttr()
              {
                Name = "objectClass"
                ,Values = new ObservableCollection<string>()
                {
                  "eagriUzivatel"
                }
              }
              ,new LdAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "alice1"
                }
              }
            }
          }
        }
        ,SearchRequests = new LdRequestsCollection()
        { 
          new LdSearchRequest()
          {
            DistinguishedName = "cn=config"
            ,Scope = LdSearchScopeEnum.wholeSubtree
            ,Filter = new FilterGroup()
            {
              Present = new PresentOp()
              {
                Name = "objectClass"
              } 
            }
          }
          ,new LdSearchRequest()
          {
            DistinguishedName = "cn=aktualizace eagri uživatelů,cn=exe"
            ,Attributes = new LdAttrNameCollection()
            {
              new LdAttrName()
              {
                Name = "member"
              }
            }
          }
          ,new LdSearchRequest()
          {
            DistinguishedName="OU=HR1,DC=Example,DC=COM"
            ,DerefAliases = LdDerefAliasesEnum.neverDerefAliases
            ,Scope = LdSearchScopeEnum.wholeSubtree
            ,TypesOnly = false

            ,Filter = new FilterGroup()
            {
              And = new AndOp()
              {
                Presents = new ObservableCollection<FilterOp>()
                {
                  new PresentOp()
                  {
                    Name = "uid"
                  }
                }
                ,Substrings = new ObservableCollection<FilterOp>()
                {
                  new SubstringsOp()
                  {
                    Name = "uid"
                    ,Initial = "B"
                  }
                }
              }
            }
            ,Attributes = new LdAttrNameCollection()
            {
              new LdAttrName(){Name = "uid"}
            }

          }
        }
        ,CompareRequests = new LdRequestsCollection()
        {
          new LdCompareRequest()
          {
            DistinguishedName = "cn=aktualizace eagri uživatelů,cn=exe"
            , Assertion = new LdAttr()
            {
              Name = "member"
              ,Values = new ObservableCollection<string>()
              {
                "CN=Alice,OU=HR1,DC=Example,DC=COM"
              }
            }
          }
        }
        ,DeleteRequests = new LdRequestsCollection()
        {
          new LdDeleteRequest()
          {
            DistinguishedName="CN=Alice,OU=HR,DC=Example,DC=COM"
          }

          ,new LdDeleteRequest()
          {
            DistinguishedName="OU=HR,DC=Example,DC=COM"
            ,Controls = new LdControlCollection()
            {
              new LdControl()
              {
                Type = "1.2.840.113556.1.4.805"
              }
            }
          }
        }
        ,ModifyRequests = new LdRequestsCollection()
        {
          new LdModifyRequest()
          {
            DistinguishedName="CN=Bob,OU=HR1,DC=Example,DC=COM"
            ,Attributes = new LdModifyAttrCollection()
            {
              new LdModifyAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "bob"
                  //,"bob-d"
                }
                ,Operation = LdAttrModifyOperationEnum.replace
              }
            }
          }
          ,new LdModifyRequest()
          {
            DistinguishedName="CN=Bob,OU=HR1,DC=Example,DC=COM"
            ,Attributes = new LdModifyAttrCollection()
            {
              new LdModifyAttr()
              {
                Name = "uid"
                ,Values = new ObservableCollection<string>()
                {
                  "bob-d"
                  ,"bob-e"
                }
                ,Operation = LdAttrModifyOperationEnum.replace
              }
            }
          }
          ,new LdModifyRequest()
          {
            DistinguishedName="cn=aktualizace eagri uživatelů,cn=exe"
            ,Attributes = new LdModifyAttrCollection()
            {
              new LdModifyAttr()
              {
                Name = "member"
                ,Values = new ObservableCollection<string>()
                {
                  "CN=Alice,OU=HR1,DC=Example,DC=COM"
                }
                ,Operation = LdAttrModifyOperationEnum.add
              }
            }
          }

        }
        ,ModifyDnRequests = new LdRequestsCollection()
        {
          new LdModifyDnRequest()
          {
            DistinguishedName = "cn=aktualizace eagri uživatelů,cn=exe"
            ,DeleteOldRDN = true
            ,NewSuperior = "cn=exe"
            ,NewRDN = "cn=aktualizace eagri uživatelů 1"
          }

        }

      };

      Console.WriteLine(rq.ToXml().InnerXml.ToString());

      //try
      //{
      //  rd = LdRequestDocument.Parse(dsmlRequest);
      //}
      //catch (Exception e)
      //{
      //  throw new LdInvalidDocumentException(e.Message);
      //}

      string strMsg = @"
<batchRequest
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  xmlns='urn:oasis:names:tc:DSML:2:0:core'
  processing='Sequential'
  onError='Resume' responseOrder='Unordered'
>
  <addRequest dn='cn=vlastnostObj,cn=schema,cn=config'>
    <attr name='objectClass'>
      <value>top</value>
    </attr>
    <attr name='mod'>
      <value>STRUCTURAL</value>
    </attr>
  </addRequest>
</batchRequest>
";

      XmlDocument xdoc = new XmlDocument();
      xdoc.LoadXml(strMsg.Replace('\'','"'));

      rq = LdRequestDocument.Parse(xdoc);

      LdResponseDocument outP = LdRequestServerExtension.ProcessBatch(rq);

      Console.WriteLine(outP.ToXml().InnerXml.ToString());


    }






  }

}
